package cn.itsource.controller;

import cn.itsource.domain.Teacher;
import cn.itsource.query.TeacherQuery;
import cn.itsource.service.ITeacherService;
import cn.itsource.utils.ExcelUtil;
import cn.itsource.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 教师接口类
 */
@RestController
@RequestMapping("/teacher") //请求路径  匹配 后端接口
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    /**
     * 查询所有教师
     * @return
     */
    @GetMapping
    public R findAll(){
        List<Teacher> teachers = teacherService.findAll();
        return R.ok(teachers);
    }

    /**
     * 查询单个对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findOne(@PathVariable("id")Integer id){
        Teacher teacher = teacherService.findOne(id);
        return R.ok(teacher);
    }

    /**
     * 添加或修改
     * @param teacher
     * @return
     */
    @PutMapping
    public R save(Teacher teacher){
        try {
            if(teacher.getId() == null){//添加
                teacherService.add(teacher);
            }else{
                teacherService.update(teacher);
            }
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息 - 方便排错
            return R.fail();
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id")Integer id){
        try {
            teacherService.delete(id);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息 - 方便排错
            return R.fail();
        }
    }

    /**
     * 分页+高级查询
     * @param teacherQuery
     * @return
     */
    @PostMapping
    public R queryPage(/*@RequestBody */TeacherQuery teacherQuery){
        Map<String, Object> map = teacherService.queryPage(teacherQuery);
        return R.ok(map);
    }
    //一个Controller中的接口请求不能一样：请求地址 + 请求参数 - 启动报错

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public R patchDel(@RequestBody Integer[] ids){
        try {
            teacherService.patchDel(ids);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息 - 方便排错
            return R.fail();
        }
    }

    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<Teacher> teachers = teacherService.findAll();
        ExcelUtil.exportExcel(teachers,"老师信息","教师",Teacher.class,"teacher",response);
    }

    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<Teacher> teachers = new ArrayList<>();
        ExcelUtil.exportExcel(teachers,"老师信息","教师",Teacher.class,"teacher",response);
    }

    //http://localhost/teacher/importExcel
    @PostMapping("/importExcel")
    public R importExcel(MultipartFile file) throws IOException {
        List<Teacher> teachers = ExcelUtil.importExcel(file, 1, 1, Teacher.class);
        //1.后续可以做批量添加
        //但是现在测试以下
        teachers.forEach(System.out::println);
        //导出不能返回R，因为导出用了response响应，R也是响应，两个响应冲突了，前端接收哪一个响应？
        //导入中没有使用过response，只返回R，不会冲突
        return R.ok("导入成功");
    }

}
